﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;

namespace JCW.Common
{
    /// <summary>
    /// 系统常用变量量
    /// </summary>
    public static class WebVariable
    {
        #region Url主机头属性 [如：www.91.com]
        /// <summary>
        /// Url主机头属性 [如：www.91.com]
        /// </summary>
        /// <remarks>如：www.91.com</remarks>
        public static string UrlHost
        {
            get
            {
                return HttpContext.Current.Request.Url.Host;
            }
        }
        #endregion

        #region Url主机端口属性 [如：80]
        /// <summary>
        /// Url主机端口属性 [如：80]
        /// </summary>
        /// <remarks>如：80</remarks>
        public static int UrlPort
        {
            get
            {
                return HttpContext.Current.Request.Url.Port;
            }
        }
        #endregion

        #region Url后缀属性 [如：www.91.net:8080/91]
        /// <summary>
        /// Url后缀属性 [如：www.91.net:8080/91]
        /// </summary>
        /// <remarks>如：www.91.net:8080/91</remarks>
        public static string UrlSuffix
        {
            get
            {
                if (UrlPort == 80)
                {
                    return UrlHost + ApplicationPath;
                }
                else
                {
                    return UrlHost + ":" + UrlPort.ToString() + ApplicationPath;
                }
            }
        }
        #endregion

        #region 远程客户主机端的IP主机地址属性 [如：61.234.224.224]
        /// <summary>
        /// 远程客户主机端的IP主机地址属性 [如：61.234.224.224]
        /// </summary>
        public static string UserHostAddress
        {
            get
            {
                if (CurrentContext == null)
                {
                    return "127.0.0.1";
                }
                else
                {
                    return CurrentContext.Request.UserHostAddress;
                }
            }
        }
        #endregion

        #region 普通Url属性 [如：http://www.91.com:8080]
        /// <summary>
        /// 普通Url属性 [如：http://www.91.com:8080]
        /// </summary>
        /// <remarks>如：http://www.91.com:8080</remarks>
        public static string UrlBase
        {
            get
            {
                if (UrlPort == 80)
                {
                    return @"http://" + UrlHost;
                }
                else
                {
                    return @"http://" + UrlHost + ":" + UrlPort.ToString();
                }
            }
        }
        #endregion

        #region 普通应用程序Url属性 [如：http://www.91.com:8080/91]
        /// <summary>
        /// 普通应用程序Url属性 [如：http://www.91.com:8080/91]
        /// </summary>
        /// <remarks>http://www.91.com:8080/91</remarks>
        public static string AppUrlBase
        {
            get
            {
                return @"http://" + UrlSuffix;
            }
        }
        #endregion

        #region 应用程序路径属性 [如：/91]
        /// <summary>
        /// 应用程序路径属性 [如：/91]
        /// </summary>
        /// <remarks>如：/91</remarks>
        public static string ApplicationPath
        {
            get
            {
                return HttpContext.Current.Request.ApplicationPath.EndsWith("/") ? "" : HttpContext.Current.Request.ApplicationPath;
            }
        }
        #endregion

        #region 应用程序路径映射属性 [如：F:\\91]
        /// <summary>
        /// 应用程序路径映射属性 [如：F:\\91]
        /// </summary>
        /// <remarks>如：/91</remarks>
        public static string ApplicationPathMap
        {
            get
            {
                if (CurrentContext == null)
                {
                    return System.Environment.CurrentDirectory;
                }
                return ApplicationPath == "" ? CurrentContext.Server.MapPath("/") : CurrentContext.Server.MapPath(ApplicationPath);
            }
        }
        #endregion

        #region 当前上下文属性
        /// <summary>
        /// 当前上下文属性
        /// </summary>
        public static System.Web.HttpContext CurrentContext
        {
            get
            {
                return HttpContext.Current;
            }
        }
        #endregion

        #region 当前请求的Url信息
        /// <summary>
        /// 当前请求的Url信息
        /// </summary>
        public static Uri CurrentUrl
        {
            get
            {
                if (CurrentContext != null && CurrentContext.Request.Url != null)
                {
                    return CurrentContext.Request.Url;
                }
                else
                {
                    return new Uri("http://www.91.com");
                }
            }
        }
        #endregion

        #region 客户端上次请求的Url信息
        /// <summary>
        /// 客户端上次请求的Url信息
        /// </summary>
        public static Uri CurrentUrlReferrer
        {
            get
            {
                if (CurrentContext == null)
                {
                    return new Uri("http://www.91.net");
                }
                else if (CurrentContext.Request.UrlReferrer != null)
                {
                    return CurrentContext.Request.UrlReferrer;
                }
                else
                {
                    return new Uri(AppUrlBase);
                }
            }
        }
        #endregion

        #region 当前的请请求处理类
        /// <summary>
        /// 当前的请请求处理类
        /// </summary>
        public static IHttpHandler CurrentHandler
        {
            get
            {
                return System.Web.HttpContext.Current.Handler;
            }
        }
        #endregion

        #region 当前的请请求处理页面类
        /// <summary>
        /// 当前的请请求处理页面类
        /// </summary>
        public static Page CurrentPage
        {
            get
            {
                try
                {
                    return ((Control)System.Web.HttpContext.Current.Handler).Page;
                }
                catch
                {
                    return null;
                }
            }
        }
        #endregion

        #region 当前的会话信息
        /// <summary>
        /// 当前的会话信息
        /// </summary>
        public static string CurrentSessionInfo
        {
            get
            {
                string sessionInfo = "";
                if (WebVariable.CurrentContext.Session != null)
                {
                    for (int i = 0; i < WebVariable.CurrentContext.Session.Count; i++)
                    {
                        sessionInfo += "\r\n\tSession[" + WebVariable.CurrentContext.Session.Keys[i] + "] = " + WebVariable.CurrentContext.Session[i].ToString();
                    }
                }

                return sessionInfo;
            }
        }
        #endregion

        #region 当前的会话信息
        /// <summary>
        /// 当前的会话信息
        /// </summary>
        public static System.Web.SessionState.HttpSessionState CurrentSession
        {
            get
            {
                return WebVariable.CurrentContext.Session;
            }
        }
        #endregion

        #region 当前用户名标识
        /// <summary>
        /// 当前用户名标识
        /// </summary>
        public static System.Security.Principal.IIdentity CurrentIdentity
        {
            get
            {
                return CurrentPage.User.Identity;
            }
        }
        #endregion

        #region 当前用户名
        /// <summary>
        /// 当前用户名
        /// </summary>
        public static string CurrentUserName
        {
            get
            {
                return CurrentPage.User.Identity.Name;
            }
        }
        #endregion
    }

}